package net.getbang.admin.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;

import net.getbang.admin.entity.SysMenu;
import net.getbang.admin.entity.SysUser;
import net.getbang.admin.mapper.SysMenuMapper;
import net.getbang.admin.mapper.SysUserMapper;
import net.getbang.admin.service.SysUserRoleService;
import net.getbang.admin.service.SysUserService;
import net.getbang.common.Constant;
import net.getbang.common.redis.SysUserRedis;

@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {

	@Autowired
	private SysMenuMapper sysMenuMapper;
	
	@Autowired
	private SysUserMapper sysUserMapper;
	@Autowired
	private SysUserRedis sysUserRedis;
	
	@Autowired
	private  SysUserRoleService sysUserRoleService;
	
	@Override
	public Set<String> getUserPermissions(Long userId) {
		List<String> permsList;
		if (userId == Constant.SUPER_ADMIN) {

			List<SysMenu> menuList = sysMenuMapper.selectList(null);
			permsList = new ArrayList<>(menuList.size());
			for (SysMenu menu : menuList) {
				permsList.add(menu.getPerms());
			}

		} else {

			permsList = this.baseMapper.queryAllPerms(userId);

		}
		// 用户权限列表
		Set<String> permsSet = new HashSet<>();
		for (String perms : permsList) {
			if (StringUtils.isBlank(perms)) {
				continue;
			}
			permsSet.addAll(Arrays.asList(perms.trim().split(",")));
		}
		return permsSet;
	}

	@Override
	public SysUser queryByUserName(String username) {

		SysUser user = new SysUser();
		
		user.setUsername(username);
		
		user = sysUserMapper.selectOne(user);
		
		return user;
	}

	@Override
	public Page<SysUser> querySysUserPages(Page<SysUser> page, SysUser sysUser) {
		
		page.setRecords(sysUserMapper.queryList(page,sysUser));
		
		return page;
	}

	@Override
	public int updatePassword(SysUser user, String password, String newPassword) {
		// TODO Auto-generated method stub
		
		sysUserRedis.delete(user);
		
		Map<String, Object> map = new HashMap<>();
		map.put("id", user.getId());
		map.put("password", password);
		map.put("newPassword", newPassword);
		
	
		return  sysUserMapper.updatePassword(map);
	}

	@Override
	public void update(SysUser user) {
		// TODO 更新用户
		
		sysUserRedis.delete(user);
		if(StringUtils.isBlank(user.getPassword())){
			user.setPassword(null);
		}else{
			user.setPassword(new Sha256Hash(user.getPassword(), user.getSalt()).toHex());
		}
		sysUserMapper.updateById(user);
		
		//保存用户与角色关系
		sysUserRoleService.saveOrUpdate(user.getId(), user.getRoleIdList());
		
	}

	@Override
	@Transactional
	public void save(SysUser user) {
		// TODO 保存用户
		user.setCreateTime(new Date());
		//sha256加密
		String salt = RandomStringUtils.randomAlphanumeric(20);
		user.setPassword(new Sha256Hash(user.getPassword(), salt).toHex());
		user.setSalt(salt);
		sysUserMapper.save(user);
		
		//保存用户与角色关系
		sysUserRoleService.saveOrUpdate(user.getId(), user.getRoleIdList());

		sysUserRedis.saveOrUpdate(user);
	}

	

}
